<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .container {
            width: 1226px;
            margin: 50px auto;
            display: flex;
            min-height: 500px;
            /* background-color: #f0f0f0; */
        }

        .preview-wrap {
            width: 400px;
            /* background-color: #ccc; */
            position: relative;
        }

        .preview {
            width: 400px;
            height: 400px;
            background-color: orange;
            position: relative;
        }

        .preview .shadow {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            left: 0;
            top: 0;
        }

        .preview img {
            width: 100%;
            height: 100%;
        }

        .preview-scale {
            width: 400px;
            height: 400px;
            position: absolute;
            left: -webkit-calc(100% + 10px);
            top: 0;
            background-color: rgba(125, 255, 255, 0.5);
            /* overflow: hidden; */
        }

        .preview-scale img {
            width: 800px;
            height: 800px;
            position: absolute;
            left: 0;
            top: 0;
            z-index: -1;

        }

        .itemInfo-wrap {
            flex: 1;
            margin-left: 10px;
            /* background-color: pink; */
        }
    </style>
</head>

<body>
    <!-- .magnifier -->
    <div class="container">
        <div class="preview-wrap">
            <div class="preview">
                <img class="smallImg" src="../images/girlsmall1.jpg" alt="">
                <div class="shadow"></div>
            </div>
            <div class="preview-scale">
                <img class="bigImg" src="../images/girlbig1.jpg" alt="">
            </div>
        </div>
        <div class="itemInfo-wrap"></div>
    </div>
</body>
<script>
    /* 
        放大镜的实现步骤:
        1. 呈比例关系放大 (布局相关=>推算出bigImg的宽高)
           shadow(200)           smallImg(400)        x
        ————————————————   ==   ————————————————  =  ————— 
        previewScalse(400)        bigImg(=> )         X

        2. 左边移动的是shadow  右边移动的是bigImg (布局相关=>推算出bigImg要定位)

        3. 移动的距离也是等比例且反向的

    
    
    */


    var previewWrap = document.getElementsByClassName("preview-wrap")[0];
    var preview = document.getElementsByClassName("preview")[0];
    var shadow = document.getElementsByClassName("shadow")[0];
    var smallImg = document.getElementsByClassName("smallImg")[0];
    var bigImg = document.getElementsByClassName("bigImg")[0];

    var maxLeft = preview.clientWidth - shadow.clientWidth;
    var maxTop = preview.clientHeight - shadow.clientHeight;

    var scale = bigImg.clientWidth / smallImg.clientWidth; // 2


    preview.onmousemove = function (e) {
        var e = e || window.event;

        var x = e.pageX - previewWrap.offsetLeft - shadow.clientWidth / 2;
        var y = e.pageY - previewWrap.offsetTop - shadow.clientHeight / 2;

        if (x < 0) x = 0;
        if (x > maxLeft) x = maxLeft;
        if (y < 0) y = 0;
        if (y > maxTop) y = maxTop;

        shadow.style.left = x + "px";
        shadow.style.top = y + "px";

        bigImg.style.left = - scale * x + "px";
        bigImg.style.top = - scale * y + "px";

    }





</script>

</html>